package cn.edu.cqu.bysj.wm.gui.table;


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import mySwingToolkit.controller.TableController;
import mySwingToolkit.model.ClassModel;
import mySwingToolkit.model.SwitchedTableModel;
import mySwingToolkit.ui.TableView;
import cn.edu.cqu.bysj.wm.gui.result.CommunityKeyNodeRow;
import cn.edu.cqu.bysj.wm.gui.table.CommunityView.ContentButton;

public class CommunityKeyNodeView extends TableView<CommunityKeyNodeRow>{
	/**
	 * create community key node view table
	 * @param title
	 * @param size
	 */
	public CommunityKeyNodeView(String title,int size){
		//构造
		super(new SwitchedTableModel<CommunityKeyNodeRow>(
				new Object[][] {
				},
				new String[] {
						"社区id", "社区大小", "关键节点个数","社区内关键节点信息"
				},
				new Class[] {
						Long.class, Integer.class, Integer.class,ContentButton.class
				},
				new boolean[] {
						false, false, false,false
				},
				false, false, false,size
				){
			@Override
			public Object[] translate(
					ClassModel<CommunityKeyNodeRow> object) {
				CommunityKeyNodeRow c=object.getObject();
				
				return new Object[]{object,this.getRowCount()+1,c.getCid(),c.getSize(),c.getKeyNodesNumber(),new ContentButton(c.getKeyNodes())};
			}
			@Override
			public CommunityKeyNodeRow constructObject(int row,
					int col) {
				@SuppressWarnings("unchecked")
				ClassModel<CommunityKeyNodeRow> cm=(ClassModel<CommunityKeyNodeRow>) this.getValueAt(row, 0);
				CommunityKeyNodeRow cr=null;
				if(cm!=null){
					cr=cm.getObject();
				}else{
					cr=new CommunityKeyNodeRow();
				}
				switch(col){
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				case 5:
					break;
				case 6:
					break;
				}
				return cr;
			}
			@Override
			public void initNullRow(int row) {
			}
		},title
		);
		//加入数据
		this.initDatas(Collections.singletonList(new CommunityKeyNodeRow()));
		//设置tc
		this.setTableController(new TableController<CommunityKeyNodeRow>(){
			@Override
			public List<CommunityKeyNodeRow> getBackupDatas(
					SwitchedTableModel<CommunityKeyNodeRow> tm) {
				JOptionPane.showMessageDialog(null, "暂不支持");
				return null;
			}
			@Override
			public boolean saveChanges(SwitchedTableModel<CommunityKeyNodeRow> tm) {
				System.out.println(tm.getInsertDatas().size());
				return true;
			}
		});
		
			//添加render
				this.getTable().getColumnModel().getColumn(5).setCellRenderer(new ContentButton("社区内关键成员列表"));
				this.getTable().addMouseListener(new JTableButtonMouseListener(this.getTable()));
	}
	public static class JTableButtonMouseListener extends MouseAdapter {
		  private final JTable table;
				
		  public JTableButtonMouseListener(JTable table) {
		    this.table = table;
		  }

		  @Override public void mouseClicked(MouseEvent e) {
		    int column = table.getColumnModel().getColumnIndexAtX(e.getX());
		    int row    = e.getY()/table.getRowHeight(); 

		    if (row < table.getRowCount() && row >= 0 && column ==5) {
		      Object value = table.getValueAt(row, column);
		      if (value instanceof JButton) {
		        ((JButton)value).doClick();
		      }
		    }
		  }
		}

}
